#pragma semicolon 1
#include <sourcemod>
#include <sdktools>

static bool:isVehicleReady = false;

public OnPluginStart()
{
	HookEvent("round_start", Event_RoundStart);
	HookEvent("tank_spawn", Event_TankSpawn);
	HookEvent("finale_escape_start", Event_RescueVehicle);
}

public Action:Event_RoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
	isVehicleReady = false;
	ResetConVar(FindConVar("director_tank_lottery_selection_time"));
}

public Action:Event_RescueVehicle(Handle:event, const String:name[], bool:dontBroadcast)
{
	isVehicleReady = true;
	
	SetConVarFloat(FindConVar("director_tank_lottery_selection_time"), 0.0);
	
	for (new i = 1; i <= MaxClients; i++)
	{
		if (IsClientInGame(i)
		&& GetClientTeam(i) == 3
		&& GetEntProp(i, Prop_Send, "m_zombieClass") == 5
		&& GetEntProp(i, Prop_Send, "m_iHealth") == RoundFloat(GetConVarInt(FindConVar("z_tank_health")) * GetConVarFloat(FindConVar("versus_tank_bonus_health"))))
		{
			ForcePlayerSuicide(i);
		}
	}
}

public Action:Event_TankSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
	if (!isVehicleReady) return;
	
	new tank = GetClientOfUserId(GetEventInt(event, "userid"));
	
	if (tank
	&& IsClientInGame(tank))
	{
		ForcePlayerSuicide(tank);
	}
}